#include "test.h"
#include "delay.h"
#include "sys.h"
#include "gui.h"
#include "stdlib.h"
#include "binary.h"
#include "system.h"

const u8 Pic0[] = {
  B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,
  B11110000,B00000000,B00000000,B00000000,
  B10001111,B00000000,B00000000,B00000000,
  B11000000,B11110000,B00000000,B00000000,
  B10100001,B00001111,B00000000,B00011100,
  B10010010,B00000100,B11110000,B00111110,
  B10001100,B00000011,B00111111,B11111111,
  B10001100,B00000011,B00111111,B11111111,
  B10010010,B00000100,B11110000,B00111110,
  B10100001,B00001111,B00000000,B00011100,
  B11000000,B11110000,B00000000,B00000000,
  B10001111,B00000000,B00000000,B00000000,
  B11110000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000,
  B00000000,B00000000,B00000000,B00000000
};

const u8 Pic1[] = {
  B00000111,B11100000,B00000000,B00000000,
  B00000100,B10110000,B00000000,B00000000,
  B00000100,B11011000,B00000000,B00000000,
  B00000100,B01001100,B00000000,B00000000,
  B00000100,B01100100,B00000000,B00000000,
  B00000100,B00111100,B00000000,B00000000,
  B00000100,B00001110,B00000000,B00000000,
  B00000100,B00000011,B00000000,B00000000,
  B00000100,B00000011,B00000000,B00000000,
  B00000100,B00000011,B10000000,B00000000,
  B00000100,B00000010,B10000000,B00000000,
  B00000100,B00000011,B11000000,B00000000,
  B00000100,B00000001,B11100000,B00000000,
  B00000100,B00000000,B11110000,B00000000,
  B00000100,B00000000,B01111100,B00000000,
  B00000100,B00000000,B00001110,B00000000,
  B00000100,B00000000,B00000011,B00000000,
  B00000100,B11111111,B11110001,B00000000,
  B00000100,B11000011,B10011001,B00000000,
  B00000100,B10000001,B00001001,B10000000,
  B00000100,B10000001,B00001000,B10000000,
  B00000100,B10000001,B00001000,B10000000,
  B00000100,B10000001,B00001000,B10000000,
  B00000100,B10000001,B00001000,B10000000,
  B00000100,B11000011,B10011001,B10000000,
  B00000100,B11111111,B11110001,B00000000,
  B00000100,B00000000,B00000001,B00000000,
  B00000100,B00000000,B00000001,B00000000,
  B00000100,B00000000,B00000001,B00000000,
  B00000100,B00000001,B11111110,B00000000,
  B00000100,B00000011,B00000110,B00000000,
  B00000100,B00000100,B00001100,B00000000,
  B00000100,B00000100,B00111000,B00000000,
  B00000100,B00000100,B11100000,B00000000,
  B00000100,B00000111,B10000000,B00000000,
  B00000111,B11111000,B00000000,B00000000
};
const u8 Mask1[] = {
  B00000111,B11100000,B00000000,B00000000,
  B00000111,B11110000,B00000000,B00000000,
  B00000111,B11111000,B00000000,B00000000,
  B00000111,B11111100,B00000000,B00000000,
  B00000111,B11111100,B00000000,B00000000,
  B00000111,B11111100,B00000000,B00000000,
  B00000111,B11111110,B00000000,B00000000,
  B00000111,B11111111,B00000000,B00000000,
  B00000111,B11111111,B00000000,B00000000,
  B00000111,B11111111,B10000000,B00000000,
  B00000111,B11111111,B10000000,B00000000,
  B00000111,B11111111,B11000000,B00000000,
  B00000111,B11111111,B11100000,B00000000,
  B00000111,B11111111,B11110000,B00000000,
  B00000111,B11111111,B11111100,B00000000,
  B00000111,B11111111,B11111110,B00000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111111,B10000000,
  B00000111,B11111111,B11111111,B10000000,
  B00000111,B11111111,B11111111,B10000000,
  B00000111,B11111111,B11111111,B10000000,
  B00000111,B11111111,B11111111,B10000000,
  B00000111,B11111111,B11111111,B10000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111111,B00000000,
  B00000111,B11111111,B11111110,B00000000,
  B00000111,B11111111,B11111110,B00000000,
  B00000111,B11111111,B11111100,B00000000,
  B00000111,B11111111,B11111000,B00000000,
  B00000111,B11111111,B11100000,B00000000,
  B00000111,B11111111,B10000000,B00000000,
  B00000111,B11111000,B00000000,B00000000
};
const u8 Pic2[] = {
  B00011000,
  B00110100,
  B01001010,
  B10111011,
  B11011101,
  B01010010,
  B00101100,
  B00011000
};

const u8 Pic3[] = {
  B00000011,
  B00000011,
  B00000011,
  B00000011,
  B00000011,
  B00000111,
  B00001011,
  B00011111,
  B00101011,
  B00111111,
  B01010111,
  B01111111,
  B10101011,
  B11111111,
  B10101011,
  B01111111
};



void TEST_ShowSence(u8 *test_state)
{
  static int i = 0;
  static int x = 100, y = 0;
  static u8 type = 0;
  static u8 state = 0;
  
  *test_state = 2;
  
  switch(state)
  {
    case 0:
      if(x > 0)
      {
        TEST_ShowWindmill(90-x,5,1080-3*i);
        TEST_ShowWindmill(20-x,15,720-2*i);
        x--;
        i++;
        if(i == 360)  i = 0;
      }
      else
      {
        i = 0;
        y = 32;
        x = -36;
        state += 1;
      }
      break;
    case 1:
      TEST_ShowWindmill(90,5,1080-3*i);
      TEST_ShowWindmill(20,15,720-2*i);
      TEST_ShowCar(2*x,y-18,i,type);
      TEST_ShowCar(x,y,i,type);
      i++;
      if(i == 360)  i = 0;
      if(i%2 == 1)  x++;
      if(x > 128)
      {
        x = -36;
        type = !type;
      }
      break;
    default:
      state = 0;
      break;
  }
}

void TEST_ShowCar(int x, int y, float ang, u8 type)
{
  POINT point;
  if(type)  GUI_ShowBMP(x,y,x+36,y+32,Pic1,Transparent|L2R|U2D);
  else      GUI_ShowPNG(x,y,x+36,y+32,Mask1,Pic1,L2R|U2D);
  GUI_SetSpanAng(ang);
  point = GUI_SetSpanCenter(x+4,y+23,x+8,y+27);
  GUI_ShowBMP(point.x,point.y,point.x+8,point.y+8,Pic2,Transparent|SpanSingle);
  point = GUI_SetSpanCenter(x+27,y+23,x+31,y+27);
  GUI_ShowBMP(point.x,point.y,point.x+8,point.y+8,Pic2,Transparent|SpanSingle);
}

void TEST_ShowWindmill(int x, int y, float ang)
{
  u8 n = 0;
  GUI_ShowBMP(x,y,x+16,y+32,Pic0,Transparent|U2D);
  for(n = 0; n < 4; n++)
  {
    GUI_SetSpanAng(ang+n*90);
    GUI_ShowBMP(x+8,y+3,x+8+16,y+3+8,Pic3,Transparent|SpanSingle|U2D);
  }
}

void TEST_ShowLine(u8 *test_state)
{
  static u16 i = 0;
  static u8 state = 0;
  
  if(system_config.state & SYSTEM_STATE_PROCESS_QUIT)
  {
    i = 0;
    state = 0;
  }
  
  switch(state)
  {
    case 0:
      GUI_DrawLine(0,0,i,128,1);
      break;
    case 1:
      GUI_DrawLine(128,0,i+90,128,1);
      break;
    case 2:
      GUI_DrawLine(128,64,i+180,128,1);
      break;
    case 3:
      GUI_DrawLine(0,64,i+270,128,1);
      break;
    case 4:
      
      break;
    default:
      *test_state += 1;
      system_config.delay_time = 255;
      i = 0;
      state = 0;
      break;
  }
  if(i < 90)  i += 5;
  else
  {
    i = 0;
    state += 1;
  }
}

void TEST_ShowChinese(u8 *test_state)
{
  POINT point;
  u8 nums[6] = {0,1,2,3,4,5};
  static int i = 0;
  static u8 state = 0;
  static u8 mode = Transparent|SpanSingle;

  if((mode&Transparent)&&(mode&Reverse))
  {
    GUI_ClearMap(0xff);
    GUI_ShowFloat(0,0,i-180,2,SIZE12,Reverse|Transparent);
  }
  else
  {
    GUI_ClearMap(0x00);
    GUI_ShowFloat(0,0,i-180,2,SIZE12,Normal);
  }

  GUI_SetSpanAng(i);
  if(mode&SpanSingle)
    point = GUI_SetSpanCenter(16,24,64,32);
  else
  {
    point.x = 16;
    point.y = 24;
  }

  GUI_ShowChineseStr(point.x,point.y,nums,6,mode);

  i+=2;
  if(i == 360)
  {
    i = 0;
    state++;
    if(state == 0)  mode = Transparent|SpanSingle;
    else if(state == 1)  mode = Transparent|Span;
    else if(state == 2)  mode = Transparent|SpanSingle|U2D|L2R|Reverse;
    else
    {
      *test_state += 1;
      i = 0;
      state = 0;
      mode = Transparent|SpanSingle;
    }
  }
}

